package com.ww.juc.jd.async.basic;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;

import com.jd.platform.async.executor.Async;
import com.jd.platform.async.wrapper.WorkerWrapper;

public class AsyncToolService {

	public Map<String, Object> queryAsync() throws ExecutionException, InterruptedException {
		TwentyFourWorker twentyFourWorker = new TwentyFourWorker();
		ConstellationWorker constellationWorker = new ConstellationWorker();

		// 二十四节气worker
		WorkerWrapper<String, String> twentyFourWrapper = new WorkerWrapper.Builder<String, String>()
			.worker(twentyFourWorker)
			.callback(twentyFourWorker)
			.param("0")
			.build();

		// 星座worker
		WorkerWrapper<String, String> constellationWrapper = new WorkerWrapper.Builder<String, String>()
			.worker(constellationWorker)
			.callback(constellationWorker)
			.param("1")
			.build();

		// 开始工作，这里设定超时时间10s
		Async.beginWork(10000L, twentyFourWrapper, constellationWrapper);

		// 打印当前线程数
		System.out.println("----------------- 当前线程数 ----------------");
		System.out.println(Async.getThreadCount());

		// 返回
		Map<String, Object> map = new HashMap<>();
		map.put("twentyFour", twentyFourWrapper.getWorkResult());
	    map.put("constellation", constellationWrapper.getWorkResult());

	    return map;
	}

	public static void main(String[] args) throws ExecutionException, InterruptedException {
		AsyncToolService asyncToolService = new AsyncToolService();
		asyncToolService.queryAsync();
	}
}
